﻿@{ 
    Layout = "_AdminBareLayout";

    string returnUrl = ViewBag.ReturnUrl ?? Url.Action("Index", "Home", new { area = "Admin" });
    string postUrl = Url.Action("RestartApplication");
    string pingUrl = Url.Content("~/taskscheduler/noop");
    string pathBase = Request.PathBase.Value.NullEmpty() ?? "/";
}

<style sm-target-zone="stylesheets" data-origin="RestartApplication">
    .cph:after {
        position: absolute;
        content: '';
        left: 0;
        right: 0;
        top: 0;
        bottom: 0;
        z-index: -1;
        background-image: url('/admin/images/bg-mobile.jpg');
        background-repeat: no-repeat;
        background-position: center 0;
        background-size: 100%;
        filter: blur(16px);
    }

    @@media (min-width: 800px) {
        .cph:after {
            background-image: url('/admin/images/bg-tablet.jpg');
        }
    }

    @@media (min-width: 1200px) {
        .cph:after {
            background-image: url('/admin/images/bg-desktop.jpg');
        }
    }
</style>

<script sm-target-zone="scripts" data-origin="RestartApplication">
    $(function () {
        let interval;
        let restartInProgress = toBool(_.getCookie(".Smart.RestartingApp"));

        function restartApplication() {
            $.ajax({
                type: "POST",
                cache: false,
                url: "@postUrl",
                global: false,
                complete() {
                    // Set cookie with 1 min. expiration
                    _.setCookie(".Smart.RestartingApp", "true", "@pathBase", new Date(Date.now() + 60000));
                    interval = setInterval(checkAlive, 500);
                }
            });
        }

        function checkAlive() {
            $.ajax({
                type: "GET",
                cache: false,
                url: "@pingUrl",
                global: false,
                success() {
                    clearInterval(interval);
                    _.setCookie(".Smart.RestartingApp", null);
                    location.replace("@returnUrl");
                }
            });
        }

        $.throbber.show({
            message: @T("Admin.Header.RestartApplication.Progress").JsValue + "...",
            speed: 0,
            white: false,
            callback() {
                if (restartInProgress) {
                    interval = setInterval(checkAlive, 1000);
                }
                else {
                    restartApplication();
                }
            }
        });
    });
</script>
